package artid.com.arttogo.fragment;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.HorizontalScrollView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;
import android.widget.TextView;

import java.util.ArrayList;

import artid.com.arttogo.R;
import artid.com.arttogo.utils.VeDate;
//关注与动态
public class AttentionAndDynamicsActivity extends BaseFragment implements OnClickListener {
	
	/** 导航栏 */
	private HorizontalScrollView mHorizontalScrollView;
	/** 导航条目 */
	private LinearLayout mLinearLayout;
	/** 导航底部线条 */
	private ImageView mImageView;
	/** 页面展示ViewPager */
	private ViewPager pager;
	/** 屏幕宽度 */
	private int mScreenWidth;
	/** 导航条目宽度 */
	private int item_width;
	/** 导航栏箭头 */
	// private ImageView rawImage;

	/** 导航栏动画相关参数 */
	private int endPosition;
	private int beginPosition;
	private int currentFragmentIndex;
	private boolean isEnd;

	/** 主页加载的Fragment */
	private ArrayList<Fragment> fragments;
	
	private View mView;
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
							 Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		
		mView = inflater.inflate(R.layout.activity_attention_dynamics, null);
		
		DisplayMetrics dm = new DisplayMetrics();
		getActivity().getWindowManager().getDefaultDisplay().getMetrics(dm);
		mScreenWidth = dm.widthPixels;
		mHorizontalScrollView = (HorizontalScrollView) mView.findViewById(R.id.hsv_view);
		mLinearLayout = (LinearLayout) mView.findViewById(R.id.hsv_content);
		mImageView = (ImageView) mView.findViewById(R.id.img1);
		item_width = (int) ((mScreenWidth / 2.0));
		mImageView.getLayoutParams().width = item_width+10;
		pager = (ViewPager) mView.findViewById(R.id.pager);
		
		mBack = (ImageView) mView.findViewById(R.id.back_btn);
		mTittle = (TextView) mView.findViewById(R.id.tittle_text);
		mBack.setVisibility(View.GONE);
		mTittle.setText("关注动态");
		initNav();
		initViewPager();
//		radioGroup = (RadioGroup) mView.findViewById(R.id.radiogroup);
//		radioGroup.setOnCheckedChangeListener(this);
//		v1 = mView.findViewById(R.id.system_message);
//		v1.setVisibility(View.GONE);
//		v2 = mView.findViewById(R.id.personal_message);
//		v2.setVisibility(View.GONE);
		return mView;
	}
	
	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onActivityCreated(savedInstanceState);
//		initFragmentManger();
		// radioGroup.setOnCheckedChangeListener(this);
//		((RadioButton) radioGroup.getChildAt(tabSelection)).toggle();
//		onTabSelect(mInfo_atten);
	}
	
	
	private void initViewPager() {
		fragments = new ArrayList<Fragment>();
		for (int i = 0; i < 2; i++) {
			
			if (i == 0) {
				AttentionDynamicsActivity fragment = new AttentionDynamicsActivity();
			
				fragments.add(fragment);
			}
			if (i == 1) {
				MyFollowActivity fragment = new MyFollowActivity();
				fragments.add(fragment);
			}
			
		}
		MyFragmentPagerAdapter fragmentPagerAdapter = new MyFragmentPagerAdapter(
				getChildFragmentManager(), fragments);
		pager.setAdapter(fragmentPagerAdapter);
		fragmentPagerAdapter.setFragments(fragments);
		pager.setOnPageChangeListener(new MyOnPageChangeListener());
		pager.setCurrentItem(0);
	}

	private void initNav() {
		String[] arrTag = { "关注动态", "我的关注"};
		mLinearLayout.removeAllViews();
		for (int i = 0; i < 2; i++) {
			RelativeLayout layout = new RelativeLayout(getActivity());
			TextView view = new TextView(getActivity());
			view.setText(arrTag[i]);
			view.setTextSize(TypedValue.COMPLEX_UNIT_SP,16);
			RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
					LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
			params.addRule(RelativeLayout.CENTER_IN_PARENT);
			layout.addView(view, params);
			mLinearLayout.addView(layout, item_width, VeDate.dip2px(getActivity(), 40));
			layout.setOnClickListener(this);
			layout.setTag(i);
		}
	}
//	private void initFragmentManger() {
//		// rgHome.setChecked(true);
//		fragmentManager = getChildFragmentManager();
//
//		mInfo_atten = new TabInfo(getActivity(), "atte",
//				AttentionDynamicsActivity.class, null);
//
//		mInfo_follo = new TabInfo(getActivity(), "foll",
//				MyFollowActivity.class, null);
//	}
	private class MyFragmentPagerAdapter extends FragmentPagerAdapter {
		private ArrayList<Fragment> fragments;
		private FragmentManager fm;

		public MyFragmentPagerAdapter(FragmentManager fm) {
			super(fm);
			this.fm = fm;
		}

		public MyFragmentPagerAdapter(FragmentManager fm,
				ArrayList<Fragment> fragments) {
			super(fm);
			this.fm = fm;
			this.fragments = fragments;
		}

		@Override
		public int getCount() {
			return fragments.size();
		}

		@Override
		public Fragment getItem(int position) {
			return fragments.get(position);
		}

		@Override
		public int getItemPosition(Object object) {
			return POSITION_NONE;
		}

		public void setFragments(ArrayList<Fragment> fragments) {
			if (this.fragments != null) {
				FragmentTransaction ft = fm.beginTransaction();
				for (Fragment f : this.fragments) {
					ft.remove(f);
				}
				ft.commit();
				ft = null;
				fm.executePendingTransactions();
			}
			this.fragments = fragments;
			notifyDataSetChanged();
		}

		@Override
		public Object instantiateItem(ViewGroup container, final int position) {
			Object obj = super.instantiateItem(container, position);
			return obj;
		}

	}

	public class MyOnPageChangeListener implements OnPageChangeListener {

		@Override
		public void onPageSelected(final int position) {
			Animation animation = new TranslateAnimation(endPosition, position
					* item_width, 0, 0);

			beginPosition = position * item_width;

			currentFragmentIndex = position;
			if (animation != null) {
				animation.setFillAfter(true);
				animation.setDuration(0);
				mImageView.startAnimation(animation);
				mHorizontalScrollView.smoothScrollTo((currentFragmentIndex - 1)
						* item_width, 0);
			}
		}

		@Override
		public void onPageScrolled(int position, float positionOffset,
				int positionOffsetPixels) {
			if (!isEnd) {
				if (currentFragmentIndex == position) {
					endPosition = item_width * currentFragmentIndex
							+ (int) (item_width * positionOffset);
				}
				if (currentFragmentIndex == position + 1) {
					endPosition = item_width * currentFragmentIndex
							- (int) (item_width * (1 - positionOffset));
				}

				Animation mAnimation = new TranslateAnimation(beginPosition,
						endPosition, 0, 0);
				mAnimation.setFillAfter(true);
				mAnimation.setDuration(0);
				mImageView.startAnimation(mAnimation);
				mHorizontalScrollView.invalidate();
				beginPosition = endPosition;
			}
		}

		@Override
		public void onPageScrollStateChanged(int state) {
			if (state == ViewPager.SCROLL_STATE_DRAGGING) {
				isEnd = false;
			} else if (state == ViewPager.SCROLL_STATE_SETTLING) {
				isEnd = true;
				beginPosition = currentFragmentIndex * item_width;
				if (pager.getCurrentItem() == currentFragmentIndex) {
					// 未跳入下一个页面
					mImageView.clearAnimation();
					Animation animation = null;
					// 恢复位置
					animation = new TranslateAnimation(endPosition,
							currentFragmentIndex * item_width, 0, 0);
					animation.setFillAfter(true);
					animation.setDuration(1);
					mImageView.startAnimation(animation);
					mHorizontalScrollView.invalidate();
					endPosition = currentFragmentIndex * item_width;
				}
			}
		}

	}

	@Override
	public void onClick(View v) {
		pager.setCurrentItem((Integer) v.getTag());
	}

	/**
	 * 获取点击的Item的对应View，
	 * 
	 * @param view
	 * @return
	 */
	private ImageView getView(View view) {
		view.destroyDrawingCache();
		view.setDrawingCacheEnabled(true);
		Bitmap cache = Bitmap.createBitmap(view.getDrawingCache());
		view.setDrawingCacheEnabled(false);
		ImageView iv = new ImageView(getActivity());
		iv.setImageBitmap(cache);
		return iv;
	}
//	private void onTabSelect(TabInfo tab) {
//		mTransaction = fragmentManager.beginTransaction();
//		onTabUnselected(mLastTabInfo, mTransaction);
//		mLastTabInfo = tab;
//		if (tab.fragment == null||tab.tag.equals("atte")) {
//			tab.fragment = (BaseFragment) Fragment.instantiate(tab.activity,
//					tab.clss.getName(), tab.args);
//			mTransaction.add(R.id.personal_fragment, tab.fragment, tab.tag);
//		} else {
//			tab.fragment.onCreate(tab.args);
//			// tab.fragment.notifyDataSetChanged();
//			mTransaction.show(tab.fragment);
//		}
//		mTransaction.commitAllowingStateLoss();
//	}

//	/**
//	 * onTabUnselected:(切换Fragment). <br/>
//	 * 
//	 * @author hushuan
//	 * @param tab
//	 * @param ft
//	 */
//	private void onTabUnselected(TabInfo tab, FragmentTransaction ft) {
//		if (tab != null && tab.fragment != null) {
//			ft.hide(tab.fragment);
//		}
//	}
//
//	@Override
//	public void onCheckedChanged(RadioGroup group, int checkedId) {
//		switch (checkedId) {
//		case R.id.radio_news:
//			mTittle.setText("关注动态");
//			onTabSelect(mInfo_atten);
//			state = 0;
//			break;
//			
//		case R.id.radio_me:
//			mTittle.setText("我的关注");
//			onTabSelect(mInfo_follo);
//			state = 1;
//			break;
//		default:
//			break;
//		}
//	}

	
}
